﻿using System;

namespace JoelAbrahamsson.Web.ExtensionMethods
{
    public static class DateTimeExtensions
    {
        public static string ToSiteStandardString(this DateTime dateTime)
        {
            return dateTime.ToString("MMMMM dd yyyy");
        }

        public static string ToTimeAgoString(this DateTime dateTime)
        {
            TimeSpan timeAgo = DateTime.Now.Subtract(dateTime);

            if (timeAgo.Days > 365)
                return string.Format("{0} years ago", timeAgo.Days / 365);

            if (timeAgo.Days > 30)
                return string.Format("{0} months ago", timeAgo.Days/30);

            if(timeAgo.Days > 0)
                return string.Format("{0} days ago", timeAgo.Days);

            if (timeAgo.Hours > 1)
                return string.Format("{0} hours ago", timeAgo.Hours);

            if (timeAgo.Hours > 0)
                return "1 hour ago";

            if (timeAgo.Minutes > 1)
                return string.Format("{0} minutes ago", timeAgo.Minutes);

            if (timeAgo.Minutes > 0)
                return "1 minute ago";

            return "Less than one minute ago";
        }

        public static string ToEntryPublicationDateString(this DateTime dateTime)
        {
            TimeSpan timeAgo = DateTime.Now.Subtract(dateTime);

            if (timeAgo.Days > 2)
                return dateTime.ToSiteStandardString();

            if (timeAgo.Days < 0)
                return dateTime.ToSiteStandardString();

            if (timeAgo.Days > 1 || dateTime.Date != DateTime.Now.Date)
                return "yesterday";

            return "today";
        }
    }
}
